home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n4.arc / TSRMEM3.C < prev    next >
C/C++ Source or Header  |  1990-09-13  |  3KB  |  118 lines

  1. #include <alloc.h>
  2. #include <dos.h>
  3. #include <stdlib.h>
  4.  
  5. /*
  6. Determine if we are using a large data model 
  7. (COMPACT, LARGE, or HUGE).  The memory map for a 
  8. large data model differs from that for a small data 
  9. model in that the heap comes after, not before, the 
  10. stack.  This code will compile and run for the 
  11. correct memory model.
  12. */
  13. #if sizeof(void far *) == sizeof(void *)
  14. #define LARGE_DATA
  15. #endif
  16.  
  17. /*
  18. This is a Turbo C variable that determines the size 
  19. of the stack at startup.
  20. */
  21. extern unsigned _stklen;
  22.  
  23. /*
  24. These variables define the stack segment and offset 
  25. to be used by the TSR.
  26. */
  27. unsigned my_ss, my_sp;
  28.  
  29. /*
  30. Minimum number of paragraphs required for TSR's 
  31. stack.
  32. */
  33. #define MIN_STACK  0x40
  34.  
  35. /*
  36. Install a TSR.
  37. */
  38. void install_tsr(unsigned heap, unsigned stack)
  39. {
  40. char *_eodata;
  41. unsigned EODATA, keeplen;
  42.  
  43. /*
  44. In a large data model, the size of the stack cannot 
  45. be changed since it is "sandwiched" between the 
  46. global data segment and the heap.  To set the size 
  47. of the stack (which is initialized by the startup 
  48. code), use the following declaration somewhere in 
  49. your code:
  50.  
  51. unsigned _stklen = STACK_SIZE;
  52.  
  53. e.g. To allocate a 1k stack:
  54. unsigned _stklen = 1024;
  55.  
  56. Changing _stklen itself will not change the size of 
  57. the stack and may have disastrous results, as it is 
  58. used by this routine.
  59. */
  60. #if !defined(LARGE_DATA)
  61. /*
  62. Make sure we have enough stack space.
  63. */
  64. stack = max(MIN_STACK, stack);
  65. #endif
  66.  
  67. /*
  68. Get end-of-data segment.  If the end of the data 
  69. segment is not on a paragraph boundary, adding 15 
  70. (0xF) bytes will push it over to the next paragraph.
  71. */
  72. _eodata = sbrk(0);
  73. _eodata += 0xF;
  74.  
  75. /*
  76. Find the next paragraph available after the TSR.
  77.  
  78. FP_SEG() and FP_OFF() are Turbo C macros that 
  79. determine the segment and offset of a far pointer.  
  80. If the pointer is a near pointer, FP_SEG() casts it 
  81. to a far pointer and returns the value of the 
  82. appropriate segment register (DS). The paragraph 
  83. number is normalized by adding the number of 
  84. paragraphs included in the offset.
  85. */
  86. EODATA = FP_SEG(_eodata) + (FP_OFF(_eodata) >> 4);
  87.  
  88. #if defined(LARGE_DATA)
  89. /*
  90. Amount of memory required is (program size + heap).
  91. */
  92. keeplen = EODATA - _psp + heap;
  93.  
  94. /*
  95. Use existing stack (i.e. stack that was set up by 
  96. the startup code).
  97. */
  98. my_ss = _SS;
  99. my_sp = _stklen;
  100.  
  101. #else
  102.  
  103. /*
  104. Amount of memory required is (program size + heap + 
  105. stack).
  106. */
  107. keeplen = EODATA - _psp + heap + stack;
  108.  
  109. /*
  110. Stack comes after data segment and heap and grows 
  111. down in memory.
  112. */
  113. my_ss = EODATA;
  114. my_sp = (heap + stack) * 16;
  115. #endif
  116.  
  117. keep(0, keeplen);
  118. }